package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.GroupDAO;
import org.millioncall.yueyoga.admin.dao.RoleDAO;
import org.millioncall.yueyoga.admin.model.Group;

public class GroupDAOImpl extends BaseDao implements GroupDAO {
	public static final String NAME = "name";
	public static final String ENABLED = "enabled";
	private RoleDAO roleDAO;

	public RoleDAO getRoleDAO() {
		return roleDAO;
	}

	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	@Override
	public void save(Group instance) {
		Map<String, Object> roles = new HashMap<String, Object>();
		roles.put("id", instance.getId());
		roles.put("name", instance.getName());
		roles.put("namecn", instance.getNamecn());
		roles.put("description", instance.getDescription());
		roles.put("enabled", instance.getEnabled());
		this.getPagableSqlSessionTemplate().insert("insert", roles);
	}

	@Override
	public void delete(Group instance) {
		this.getPagableSqlSessionTemplate().delete("delete", instance.getId());
	}

	@Override
	public Group findById(int id) {
		Group group = this.getPagableSqlSessionTemplate().selectOne("findById",
				id);
		return group;
	}

	@Override
	public List<Group> findByProperty(String propertyName, Object value) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", propertyName);
		map.put("value", value);
		List<Group> groups = this.getPagableSqlSessionTemplate().selectList(
				"findByProperty", map);
		return groups;
	}

	@Override
	public List<Group> findByName(String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "name");
		map.put("value", name);
		List<Group> groups = this.getPagableSqlSessionTemplate().selectList(
				"findByProperty", map);
		return groups;
	}

	@Override
	public List<Group> findByEnabled(Boolean enabled) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "enabled");
		map.put("value", enabled);
		List<Group> groups = this.getPagableSqlSessionTemplate().selectList(
				"findByProperty", map);
		return groups;
	}

	@Override
	public List<Group> findAllGroups() {
		List<Group> groups = this.getPagableSqlSessionTemplate().selectList(
				"findAllGroups");
		return groups;
	}

	@Override
	public List<Group> findAllGroup() {
		List<Group> group = this.getPagableSqlSessionTemplate().selectList(
				"findAllGroup");
		return group;
	}

	@Override
	public List<Group> findGroupsByNames(List<String> names, boolean needEnabled) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("names", names);
		map.put("enabled", needEnabled == true ? "1" : "0");
		List<Group> groups = this.getPagableSqlSessionTemplate().selectList(
				"findGroupsByNames", map);
		return groups;
	}

	@Override
	public PaginatedList<Group> findGroupsInPage(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<Group> pq = this.getPagableSqlSessionTemplate().findAll(
				"findGroupsInPage", map);
		PaginatedList<Group> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public int saveGroup(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveGroup", map);
	}

	@Override
	public int saveGroupRole(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveGroupRole", map);
	}

	@Override
	public int savegrantRoles(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate()
				.insert("savegrantRoles", map);
	}
	
	@Override
	public int savegrantHall(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate()
				.insert("savegrantHall", map);
	}

	@Override
	public int saveGrantGroups(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveGrantGroups",
				map);
	}

	@Override
	public int updateGroup(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update("updateGroup", map);
	}

	@Override
	public int updateGroupRole(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update("updateGroupRole",
				map);
	}

	@Override
	public Group findGroupById(int id) {
		return this.getPagableSqlSessionTemplate().selectOne("findGroupById",
				id);
	}

	@Override
	public Group findGroupByName(String name) {
		return this.getPagableSqlSessionTemplate().selectOne("findGroupByName",
				name);
	}
	
	@Override
	public Group findGroupByChName(String name) {
		return this.getPagableSqlSessionTemplate().selectOne("findGroupByChName",
				name);
	}
	
	@Override
	public void deleteGroupById(int[] id) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < id.length; i++) {
			map.put("id", id[i]);
			this.getPagableSqlSessionTemplate().delete("deleteGroupById", map);
		}
	}

	@Override
	public void deleteGroupRoleById(int[] id) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < id.length; i++) {
			map.put("id", id[i]);
			this.getPagableSqlSessionTemplate().delete("deleteGroupRoleById",
					map);
		}
	}

	@Override
	public void deleteGroupUserById(int[] id) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < id.length; i++) {
			map.put("id", id[i]);
			this.getPagableSqlSessionTemplate().delete("deleteGroupUserById",
					map);
		}
	}

	@Override
	public List<Group> findByUserId(String userId) {
		return this.getPagableSqlSessionTemplate().selectList(
				"findUserGroupsByUserId", userId);
	}
	
	@Override
	public int deleteGroupUserByUserId(String userId){
		return this.getPagableSqlSessionTemplate().delete(
				"deleteGroupUserByUserId", userId);
	}
	
	public int deleteRoleById(String groupId){
		return this.getPagableSqlSessionTemplate().delete("deleteRoleById",groupId);
	}
	
	public int deleteHallById(String groupId){
		return this.getPagableSqlSessionTemplate().delete("deleteHallById",groupId);
	}
}